package com.turing.usdtpay.mvp.presenter;

import android.app.Activity;

import com.google.gson.Gson;
import com.turing.usdtpay.entity.AuthData;
import com.turing.usdtpay.http.ApiService;
import com.turing.usdtpay.http.ApiUrl;
import com.turing.usdtpay.http.callback.ApiCallback;
import com.turing.usdtpay.http.entity.ResponseDate;
import com.turing.usdtpay.mvp.contract.AuthContract;

import java.io.File;
import java.util.HashMap;

/**
 * @作者：JAY
 * @创建时间：2019/11/5 10:07
 * @描述：
 **/
public class AuthPresenter implements AuthContract.Present {

    private AuthContract.View mView;

    public AuthPresenter(AuthContract.View mView) {
        this.mView = mView;
    }

    @Override
    public void uploadImg(Activity activity, String id, final int type, File file) {
        HashMap<String,String> map = new HashMap<>();
        map.put("id","1");
        map.put("type",type+"");
        ApiService.getInstance().uploadFile(activity, ApiUrl.API_UPLOAD_IMG, map, file, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.uploadSuccess(type);
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }

            @Override
            public void onUIProgress(int progress) {
                super.onUIProgress(progress);
                mView.onProgress(progress);
            }
        });
    }

    @Override
    public void getAuthInfo(Activity activity) {
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_GET_AUTH_INFO, new HashMap<String, String>(), new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    Gson gson = new Gson();
                    AuthData authData = gson.fromJson(date.getDate(), AuthData.class);
                    mView.getSuccess(authData);
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }

    @Override
    public void saveAuthInfo(Activity activity, String name, String num) {
        HashMap<String,String> map = new HashMap<>();
        map.put("name",name);
        map.put("check_num",num);
        ApiService.getInstance().postRequestWithCommmon(activity, ApiUrl.API_SAVE_AUTH_INFO, map, new ApiCallback() {
            @Override
            public void onSuccess(int code, ResponseDate date) {
                if (date.getRet() == 1){
                    mView.saveSuccess();
                }else {
                    mView.onFail(date.getMsg());
                }
            }

            @Override
            public void onFailure(Throwable exception) {
                mView.onFail(exception.getMessage());
            }
        });
    }
}
